package com.nl.controller.web.service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.CmppReplyInfo;
import com.nl.controller.web.bean.TaskSynInfo;
import com.nl.controller.web.mapper.JzzwCheckMapper;
import com.nl.controller.web.mapper.SmsUpwardMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SmsUpwardService {
	@Autowired
	private SmsUpwardMapper smsUpwardMapper;
	@Autowired
	private CommonService commonService;
	@Autowired
	private JzzwCheckMapper jzzwCheckMapper;
	@Autowired
	private SmsInterfaceJzzwService smsInterfaceJzzwService;
	public void upward(Map<String, String> map){
		List<CmppReplyInfo> cmppReplyInfos = smsUpwardMapper.checkCmpp();
		if (cmppReplyInfos != null && !cmppReplyInfos.isEmpty()) {
			for (CmppReplyInfo cmppReplyInfo : cmppReplyInfos) {
				String srcTerminalId = cmppReplyInfo.getSrcTerminalId();
				String msgContent = cmppReplyInfo.getMsgContent().trim();
				String rowId = cmppReplyInfo.getRowId();
				if (msgContent.indexOf("+")!=-1) {
					String[] split = msgContent.split("+");
					if (split.length == 2) {
						String flag = split[0].toUpperCase();
						String mgrId = split[1].trim();
						if (srcTerminalId.startsWith("86")) {
							srcTerminalId = srcTerminalId.substring(2);
						}
						log.info("mgrId"+mgrId+msgContent);
						String[] msisdns = commonService.templateNotice("1000000002");
						List<String> msisdn = Arrays.asList(msisdns);
						if (msisdn.contains(srcTerminalId)) {
							TaskSynInfo taskInfo = smsUpwardMapper.findTaskInfo(mgrId);
							if (taskInfo == null) {
								log.info("mgrId"+mgrId+msgContent);
								continue;
							}
							Map<String, Object> checkMap = new HashMap<String, Object>();
							if ("Y".equals(flag)) {
								map.put(rowId, "id");
								checkMap.put("checkMsg", "");
								checkMap.put("mgrId", mgrId);
								checkMap.put("checkResult","1");
							}else if ("N".equals(flag)) {
								map.put(rowId, "id");
								checkMap.put("checkMsg", "");
								checkMap.put("mgrId", mgrId);
								checkMap.put("checkResult","2");
							}
							if (map!=null&&!map.isEmpty()) {
								try {
									int templateCheck = jzzwCheckMapper.templateCheck(checkMap);
									log.info("项目:"+mgrId+"审核结果入表:"+(templateCheck == 1 ? "成功" : "失败"));
									//调用JZZW审核同步接口
									JSONObject checkMsg = new JSONObject();
									checkMsg.put("taskId", mgrId);
									checkMsg.put("checkMsg", "");
									if ("Y".equals(flag)) {
									checkMsg.put("checkResult","1");	
									}else if ("N".equals(flag)) {
									checkMsg.put("checkResult","2");
									}
									smsInterfaceJzzwService.checkResultSyn(checkMsg);
								} catch (Exception e) {
									log.error("短信上行审核出现异常"+mgrId+";EXCEPTION"+e.toString());
									e.printStackTrace();
								}
							}
						}
					}
				}
			}
		}
	}
}
